package com.lebang.controller.api;

import com.lebang.domain.*;
import com.lebang.entity.RequestHeaders;
import com.lebang.example.CustomerFeedbackCategoryExample;
import com.lebang.example.CustomerMemberDiscontentExample;
import com.lebang.example.CustomerMemberIssueExample;
import com.lebang.model.enums.buss.RewardType;
import com.lebang.model.enums.buss.TradeCategory;
import com.lebang.service.*;
import com.lebang.service.buss.WalletService;
import com.lebang.service.sys.AppLookCategoryService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 帮助中心
 *
 * @author ljx
 * @date 2020/7/17
 */
@Api(tags = "帮助中心")
@RestController
@RequestMapping("/app/help")
public class AppHelpCenterApi {
    @Autowired
    private WalletService walletService;
    @Autowired
    private AppLookCategoryService appLookCategoryService;

    @Autowired
    private CustomerMemberDiscontentService customerMemberDiscontentService;
    @Autowired
    private CustomerMemberContentmentService customerMemberContentmentService;
    @Autowired
    private CustomerMemberIssueService customerMemberIssueService;
    @Autowired
    private CustomerFeedbackCategoryService customerFeedbackCategoryService;
    /**
     * 问题咨询
     */
    @ApiOperation("问题咨询")
    @PostMapping("/consult")
    public JsonData issueConsult(HttpServletRequest request) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        CustomerMemberIssueExample appIssueExample=new CustomerMemberIssueExample();
        List<CustomerMemberIssue> appIssues = customerMemberIssueService.selectByExample(appIssueExample);
        Integer all = walletService.cash(requestHeaders.getUserid(), requestHeaders.getAppid(), TradeCategory.Income.name(), RewardType.Cash.name(), null, null);
        List<CustomerMemberIssue> appIssueList= appLookCategoryService.reviewIssue(appIssues, requestHeaders, all);
        Map<String, List<CustomerMemberIssue>> collect = appIssueList.stream().collect(Collectors.groupingBy(CustomerMemberIssue::getTitle));
        List list=new ArrayList();
        for (String s : collect.keySet()) {
            Map<String,Object> map=new HashMap();
            map.put("title",s);
            map.put("list",collect.get(s));
            list.add(map);
        }
        return Callback.success("success", "查询成功", list);
    }


    /**
     * 满意
     */
    @ApiOperation("满意")
    @PostMapping("/contentment")
    public JsonData contentment(HttpServletRequest request,  String phone) {
        RequestHeaders requestHeaders = new RequestHeaders(request);

        CustomerMemberContentment customerMemberContentment = new CustomerMemberContentment();
        customerMemberContentment.setCreateTime(LocalDateTime.now());
        customerMemberContentment.setModifyTime(LocalDateTime.now());
        customerMemberContentment.setMemberId(requestHeaders.getUserid());
        customerMemberContentment.setPhone(phone);
        customerMemberContentment.setAppStore(requestHeaders.getAppStore());
        customerMemberContentment.setVersion(requestHeaders.getVersion());
        customerMemberContentment.setIpAddress(requestHeaders.getIpAddr());
        customerMemberContentment.setImei(requestHeaders.getImei());
        //满意写入数据库
        customerMemberContentmentService.insertModel(customerMemberContentment);
        return Callback.success("success", "满意");
    }

    /**
     * 不满意反馈类目
     */
    @ApiOperation("不满意反馈类目")
    @PostMapping("/feedbackname")
    public JsonData feedbackName(HttpServletRequest request) {
        //返回反馈类目
        CustomerFeedbackCategoryExample FeedbackNameExample=new CustomerFeedbackCategoryExample();
        return Callback.success("success", "不满意反馈类目",customerFeedbackCategoryService.selectByExample(FeedbackNameExample));
    }

    /**
     * 不满意信息
     */
    @ApiOperation("不满意信息")
    @PostMapping("/discontent")
    public JsonData discontent(HttpServletRequest request, String phone, @RequestParam("feedbackCategory") String feedbackCategory, @RequestParam("cause") String cause, Integer isServe, String qq) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        if (feedbackCategory == null) {
            return Callback.fail("201", "请选择反馈类目");
        }
        if (cause == null) {
            return Callback.fail("202", "请填写原因");
        }
        CustomerMemberDiscontentExample example=new CustomerMemberDiscontentExample();
        example.createCriteria().andFeedbackCategoryEqualTo(feedbackCategory).andCauseEqualTo(cause);
        List<CustomerMemberDiscontent> customerMemberDiscontents = customerMemberDiscontentService.selectByExample(example);
        if (customerMemberDiscontents.size()!=0){
            return Callback.fail("203","请勿重复反馈");
        }
        CustomerMemberDiscontent customerMemberDiscontent = new CustomerMemberDiscontent();
        customerMemberDiscontent.setCreateTime(LocalDateTime.now());
        customerMemberDiscontent.setModifyTime(LocalDateTime.now());
        customerMemberDiscontent.setMemberId(requestHeaders.getUserid());
        customerMemberDiscontent.setPhone(phone);
        customerMemberDiscontent.setCause(cause);
        customerMemberDiscontent.setQq(qq);
        customerMemberDiscontent.setAppStore(requestHeaders.getAppStore());
        customerMemberDiscontent.setFeedbackCategory(feedbackCategory);
        customerMemberDiscontent.setIsServe(isServe);
        customerMemberDiscontent.setVersion(requestHeaders.getVersion());
        customerMemberDiscontent.setIpAddress(requestHeaders.getIpAddr());
        customerMemberDiscontent.setImei(requestHeaders.getImei());

        customerMemberDiscontentService.insertModel(customerMemberDiscontent);
        return Callback.success("success", "返回成功");
    }

}
